package com.bbz.spring.config;

import com.bbz.spring.bean.Yellow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     *
     * @param importingClassMetadata 当前类的注解信息
     * @param registry BeanDefinitionRegistry注册类，把所有需要注册到容器的Bean,
     *                 通过registerBeanDefinitions 手工注册到容器中
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        if(registry.containsBeanDefinition("com.bbz.spring.bean.Blue")&& registry.containsBeanDefinition("com.bbz.spring.bean.Red")){
            //指定Bean的定义信息（Bean的类型，Bean的scope）
            RootBeanDefinition rootBeanDefinition=new RootBeanDefinition(Yellow.class);
            //注册bean,id为Yellow
            registry.registerBeanDefinition("Yellow", rootBeanDefinition);
        }
    }
}
